package com.e3.sysmg.security.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.sysmg.security.model.RoleModel;
import com.e3.sysmg.security.model.UserModel;
import com.e3.web.widget.client.MyField;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Element;

public class UserFormPanel extends LayoutContainer{
	
	private UserMessages MESSAGES = UserMessages.INSTANCE;
	
	private Map<String,Field<?>> formFieldMaps = new HashMap<String, Field<?>>();
	private FormPanel formPanel;
	private FormData formData = new FormData("100%");
	
	private UserRoleGridField roleGridField;
	
	private UserModel userModel;
	
	private String[] names = {UserModel.ID, UserModel.CODE,UserModel.NAME,UserModel.PASSWORD,UserModel.START_DATE,UserModel.END_DATE,UserModel.STATUS};
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new FitLayout());
		
		add(getFormPanel());
	}
	
	private FormPanel getFormPanel(){
		if(formPanel == null){
			formPanel = new FormPanel();
			formPanel.setHeaderVisible(false);
			initFormPanel(formPanel);
		}
		return formPanel;
	}
	
	private void initFormPanel(FormPanel form){
		FieldSet fieldSet = getBaseInfoFields();
		form.add(fieldSet);
		
		fieldSet = getRolesField();
		form.add(fieldSet);
	}
	
	private FieldSet getBaseInfoFields(){
		FieldSet fieldSet = new FieldSet();
		fieldSet.setHeading(MESSAGES.title_group_baseinfo());
		fieldSet.setLayout(new FormLayout());
		
		
		for (String name : names) {
			Field<?> field = UserHelper.INSTANCE.createFormField(name);
			fieldSet.add(field,formData);
			formFieldMaps.put(name, field);
		}
		return fieldSet;
	}
	
	private FieldSet getRolesField(){
		FieldSet fieldSet = new FieldSet();
		fieldSet.setHeading(MESSAGES.title_parent_roles());
		fieldSet.setLayout(new FitLayout());
		
		fieldSet.add(getUserRoleGridField());
		
		return fieldSet;
	}
	
	public UserRoleGridField getUserRoleGridField(){
		if(roleGridField == null){
			roleGridField = new UserRoleGridField();
		}
		return roleGridField;
	}
	
	@SuppressWarnings("rawtypes")
	public UserModel getFormValue(){
		UserModel model = userModel;
		if(model == null){
			model = new UserModel();
		}
		
		for (String name : names) {
			MyField field = (MyField)formFieldMaps.get(name);
			model.set(name, field.getFieldValue());
		}
		
		List<RoleModel> roles = getUserRoleGridField().getGridValues();
		model.setRoles(roles);
		return model;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void setFormValue(UserModel model){
		this.userModel = model;
		getFormPanel().clear();
		if(model == null){
			return;
		}
		for (String name : names) {
			MyField field = (MyField)formFieldMaps.get(name);
			field.setFieldValue(model.get(name));
		}
		
		List<RoleModel> roles = model.getRoles();
		getUserRoleGridField().setGridValues(roles);
	}

}
